package com.xu.a2019;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.Arrays;
import java.util.List;

@SuppressWarnings("ALL")
public class SqlCallBack implements SqlHelper.ICallBack {

    public static final String TABLE_NAME_PHRASE = "phrase";
    public static final String TABLE_NAME_FAVORITE = "cache";

    private static final String DATABASE_NAME = "phrase_cache.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TYPE_TEXT = " TEXT";
    private static final String TYPE_INTEGER = " INTEGER";
    private static final String SEP_COMMA = ",";

    private static final String TABLE_SCHEMA_PHRASE =
            "CREATE TABLE " + TABLE_NAME_PHRASE + " (" +
                    PhraseEntry._ID + TYPE_INTEGER + " PRIMARY KEY AUTOINCREMENT, " +
                    PhraseEntry.COLEUM_NAME_DATE + TYPE_TEXT + SEP_COMMA +
                    PhraseEntry.COLEUM_NAME_KNIGHT + TYPE_TEXT + SEP_COMMA +
                    PhraseEntry.COLEUM_NAME_ZERO + TYPE_TEXT + SEP_COMMA +
                    PhraseEntry.COLEUM_NAME_TIMESTAMP + TYPE_INTEGER + ")";
    private static final String TABLE_SCHEMA_FAVORITE =
            "CREATE TABLE " + TABLE_NAME_FAVORITE + " (" +
                    PhraseEntry._ID + TYPE_INTEGER + " PRIMARY KEY AUTOINCREMENT, " +
                    PhraseEntry.COLEUM_NAME_DATE + TYPE_TEXT + SEP_COMMA +
                    PhraseEntry.COLEUM_NAME_KNIGHT + TYPE_TEXT + SEP_COMMA +
                    PhraseEntry.COLEUM_NAME_ZERO + TYPE_TEXT + SEP_COMMA +
                    PhraseEntry.COLEUM_NAME_TIMESTAMP + TYPE_INTEGER + ")";

    public SqlCallBack() {
    }

    @Override
    public List<String> createTablesSQL() {
        return Arrays.asList(TABLE_SCHEMA_PHRASE, TABLE_SCHEMA_FAVORITE);
    }

    @Override
    public String getDatabaseName() {
        return DATABASE_NAME;
    }

    @Override
    public int getVersion() {
        return DATABASE_VERSION;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        switch (oldVersion) {
            case 0:
                db.execSQL(TABLE_SCHEMA_FAVORITE); // 升级操作；
            case 1:
                break;
            default:
                break;
        }
    }


    @Override
    public <T> void assignValuesByEntity(String tableName, T t, ContentValues values) {

        switch (tableName) {
            case TABLE_NAME_PHRASE:
                if (t instanceof PhraseCache) {
                    PhraseCache phraseCache = (PhraseCache) t;
                    values.put(PhraseEntry.COLEUM_NAME_DATE, phraseCache.getmDate());
                    values.put(PhraseEntry.COLEUM_NAME_KNIGHT, phraseCache.getmKnight());
                    values.put(PhraseEntry.COLEUM_NAME_ZERO, phraseCache.getmZero());
                    values.put(PhraseEntry.COLEUM_NAME_TIMESTAMP, phraseCache.getmTimeStamp());
                }
                break;
            case TABLE_NAME_FAVORITE:
                if (t instanceof PhraseCache) {
                    PhraseCache phraseCache = (PhraseCache) t;
                    values.put(PhraseEntry.COLEUM_NAME_DATE, phraseCache.getmDate());
                    values.put(PhraseEntry.COLEUM_NAME_KNIGHT, phraseCache.getmKnight());
                    values.put(PhraseEntry.COLEUM_NAME_ZERO, phraseCache.getmZero());
                    values.put(PhraseEntry.COLEUM_NAME_TIMESTAMP, phraseCache.getmTimeStamp());
                }
                break;
            default:
                break;
        }
    }

    @Override
    public Object newEntityByCursor(String tableName, Cursor cursor) {
        switch (tableName) {
            case TABLE_NAME_PHRASE:
                return new PhraseCache(
                        cursor.getString(cursor.getColumnIndex(PhraseEntry.COLEUM_NAME_ZERO)),
                        cursor.getString(cursor.getColumnIndex(PhraseEntry.COLEUM_NAME_DATE)),
                        cursor.getString(cursor.getColumnIndex(PhraseEntry.COLEUM_NAME_KNIGHT)),
                        cursor.getInt(cursor.getColumnIndex(PhraseEntry.COLEUM_NAME_TIMESTAMP))
                );
            case TABLE_NAME_FAVORITE:
                return new PhraseCache(
                        cursor.getString(cursor.getColumnIndex(PhraseEntry.COLEUM_NAME_ZERO)),
                        cursor.getString(cursor.getColumnIndex(PhraseEntry.COLEUM_NAME_DATE)),
                        cursor.getString(cursor.getColumnIndex(PhraseEntry.COLEUM_NAME_KNIGHT)),
                        cursor.getInt(cursor.getColumnIndex(PhraseEntry.COLEUM_NAME_TIMESTAMP))
                );
            default:
                break;
        }

        return null;
    }
}
